package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.system.common.result.Result;
import com.system.runtai.entity.AppletUser;
import com.system.runtai.entity.Enter;
import com.system.runtai.entity.Posted;
import com.system.runtai.service.AppletUserService;
import com.system.runtai.service.EnterService;
import com.system.runtai.service.PostedService;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 行业动态入驻 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-04-10
 */
@Api(tags = "行业动态入驻")
@RestController
@RequestMapping("/runtai/enter")
public class EnterController {


    @Autowired
    private EnterService enterService;

    @Autowired
    private PostedService postedService;

    @Autowired
    private AppletUserService appletUserService;

    @ApiOperation(value = "查询所有行业动态入驻")
    @GetMapping("/enterList")
    public Result<List<Enter>> enterList(){
        List<Enter> list = enterService.list();
        return Result.ok(list);
    }


    @ApiOperation(value = "入驻行业圈子")
    @PostMapping("saveEnter")
    public Result saveEnter(@RequestBody Enter enter){
        String s = AuthContextUtil.get();
        LambdaQueryWrapper<Enter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Enter::getAppletUserId,Integer.valueOf(s));
        Enter byId = enterService.getById(wrapper);
        if (byId!=null){
            //TODO  可以后续需要进行排序
            Integer start = byId.getStart();

            if (start==1) {
                List<Posted> list = postedService.list();
                return Result.ok(list);
            }
        }

        enter.setCreateTime(LocalDateTime.now());
        AppletUser byId1 = appletUserService.getById(Integer.valueOf(s));
        enter.setAvatar(byId1.getAvatar());
        boolean save = enterService.save(enter);

        if (save){
            return Result.ok().message("添加成功");
        }
        return Result.fail().message("添加失败");
    }

    @ApiOperation(value = "删除行业圈子")
    @PostMapping("delectEnter")
    public Result delectEnter(@RequestBody Enter enter){
//        String s = AuthContextUtil.get();
        boolean b = enterService.removeById(enter.getId());

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "修改行业圈子")
    @PostMapping("updateEnter")
    public Result<Enter> updateEnter(@RequestBody Enter enter){
        String s = AuthContextUtil.get();

        LambdaQueryWrapper<Enter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Enter::getAppletUserId,Integer.valueOf(s));
        enter.setUpdateTime(LocalDateTime.now());
        boolean update = enterService.update(wrapper);

        if (update){
            return Result.ok(enter).message("修改成功");
        }
        return Result.fail(enter).message("修改失败");
    }

}

